// pages/detail/detail.js
const DB = wx.cloud.database()
Page({
  data: {
    sightInfo:{},
    hasPlugin: false
  },
  goNav() {
    let plugin = requirePlugin('routePlan');
    let key = 'D5SBZ-RZW3P-TTMDP-LD2CX-GREDE-NGBBI';  //使用在腾讯位置服务申请的key
    let referer = '参观成信';   //调用插件的app的名称
    let endPoint = JSON.stringify({  //终点
      'name': this.data.sightInfo.title,
      'latitude': this.data.sightInfo.latitude,
      'longitude': this.data.sightInfo.longitude
    });
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    });
  },
  //翻转
  rotateFn(e) {
    var id = e.currentTarget.dataset.id
    this.animation_main = wx.createAnimation({
      duration: 400,
      timingFunction: 'linear'
    })
    this.animation_back = wx.createAnimation({
      duration: 400,
      timingFunction: 'linear'
    })
    // 点击打卡按钮
    if (id == 1) {
      this.animation_main.rotateY(180).step()
      this.animation_back.rotateY(0).step()
      const date = this.getTime()
      //防止在未关闭页面的情况下,再次请求打卡
      if(!this.data.hasPlugin) {
        wx.cloud.callFunction({
          name: 'plugin',
          data: {
            id: this.data.sightInfo.id
          }
        }).then(res => {
          if (res.result == 200) {
            this.setData({
              animationMain: this.animation_main.export(),
              animationBack: this.animation_back.export(),
              hasPlugin: true,
              ['sightInfo.num']: this.data.sightInfo.num + 1
            })
            wx.showToast({
              title: '打卡成功',
              icon: 'none'
            })
          } else if (res.result == 201) {
            this.setData({
              animationMain: this.animation_main.export(),
              animationBack: this.animation_back.export(),
              hasPlugin: true
            })
            wx.showToast({
              title: '今日已打卡',
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: '打卡失败，请重试',
              icon: 'none'
            })
          }
        })
      }else{
        this.setData({
          animationMain: this.animation_main.export(),
          animationBack: this.animation_back.export()
        })
        wx.showToast({
          title: '今日已打卡',
          icon: 'none'
        })
      }
    }
    // 点击背面
    else {
      this.animation_main.rotateY(0).step()
      this.animation_back.rotateY(-180).step()
      this.setData({
        animationMain: this.animation_main.export(),
        animationBack: this.animation_back.export(),
      })
    }
  },
  //获取当前年月日
  getTime() {
    const timestamp = Date.parse(new Date())
    const nowDate = new Date(timestamp)
    const year = nowDate.getFullYear();
    const month = (nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1);
    const day = (nowDate.getDate() < 10 ? '0' + nowDate.getDate() : nowDate.getDate());
    const date = year + '-' + month + '-' + day
    return date
  },
  onLoad: function (options) {
    DB.collection('sightInfo').where({ id: options.id }).get().then(res=>{
      res.data[0].num
      this.setData({
        sightInfo: res.data[0]
      })
    })
  }
})